package org.example.utils;


import com.google.common.base.CaseFormat;
import org.apache.commons.lang.StringEscapeUtils;
import org.example.annotation.NotCamel;
import org.example.annotation.Type;
import org.example.init.Context;

import java.lang.reflect.Parameter;
import java.util.Arrays;

public abstract class StrategyMethodUtil {
    private static final Strategy  strategy= new Strategy();


    abstract Object  invoke(Parameter p,Object arg0);

    abstract Object  invoke(Parameter p,Object arg0,Object arg1);

    static  class  Strategy extends StrategyMethodUtil {


      @Override
      Object invoke(Parameter p, Object arg0) {
          if(p.getType().equals(String.class))
              return StringEscapeUtils.escapeSql(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, (String) arg0));
          if(p.getType().equals(String[].class))
              return Arrays.stream(((String[]) arg0)).map(obj->StringEscapeUtils.escapeSql(CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, obj))).toArray(String[]::new);

          return arg0;
      }

      @Override
      Object invoke(Parameter p, Object arg0, Object arg1) {
          return   Context.dbHandler.doHandler(arg0,arg1);
      }
  }


    public static Object execute(Parameter p,Object arg0, Object arg1 ){
        if(!p.isAnnotationPresent(NotCamel.class)){

           return strategy.invoke(p,arg0);
        }

        if(p.isAnnotationPresent(Type.class)){
          return strategy.invoke(p,arg0,arg1);
        }
        return  arg0;
    }




}
